﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicClass;
using Xinkui.Entity.User;
using Xinkui.Entity.AutoGenerator;
using Xinkui.WebModel;
using Xinkui.WebModel.Cache;
using Xinkui.WebModel.Model;
using CommonFramework.Utility;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.WebModel.Helper;

public partial class registerstep2 : PageBase {
    protected override void BindDataSource() {
        base.BindDataSource();
        if (Session["RUserInfo"] == null || Session["RCompanyInfo"] == null) {
            Response.Redirect("registerstep1.aspx");
            Response.End();
        }

        int countryId = 11;

        if (!IsPostBack) {
            var provinceList = ProvinceCache.ListByCountry(countryId);
            if (provinceList != null && provinceList.Count > 0) {
                rptProvince.DataSource = ProvinceCache.ListByCountry(countryId).Select(u => new SelectItem {
                    Value = u.ProvinceID.ToString(),
                    Text = u.Province_EN
                });
                this.rptProvince.DataBind();
            }
            else {
                rptProvince.Visible = false;
            }

            var countryInfo = CountryCache.Get(countryId);
            if (countryInfo != null) {
                ltCountry.Text = countryInfo.Name;
            }

            this.rptBussinessType.DataSource = DictCache.Dict((int)DictTypeEnum.BusinessType).Values;

            rptBussinessType.DataBind();

            var exportMarket = DictCache.Dict((int)DictTypeEnum.MainBuyingArea).Values;



            this.rptChkMarket.DataSource = exportMarket.Select(u => new SelectItem {
                Text = u.Value,
                Value = u.Idx.ToString()
            });
            this.rptChkMarket.DataBind();
        }
    }

    protected override void HandlePostActions() {
        base.HandlePostActions();

        UserInfoMaintain_Entity userInfo = Session["RUserInfo"] as UserInfoMaintain_Entity;
        CompanyInfoMaintain_Entity companyInfo = Session["RCompanyInfo"] as CompanyInfoMaintain_Entity;


        companyInfo.ProvinceID = WebHelper.GetFormValue<int>("dropdownProvince", -1);
        companyInfo.CityID = WebHelper.GetFormValue<int>("dropdownCity", -1);
        companyInfo.StreetAddress = WebHelper.GetFormValue<string>("txtAddress", string.Empty);
        companyInfo.PostCode = WebHelper.GetFormValue<string>("txtPostCode", string.Empty);
        companyInfo.CompanyDescription = WebHelper.GetFormValue<string>("txtDescription", string.Empty);
        companyInfo.WebSite = WebHelper.GetFormValue<string>("txtWebSite", string.Empty);
        companyInfo.MainProduct = WebHelper.GetFormValue<string>("txtMainProduct", string.Empty);
        companyInfo.ExportMarket = WebHelper.GetFormValue<string>("chkMarket", string.Empty);
        companyInfo.IsOE = WebHelper.GetFormValue<int>("rbtnIsOE", 0);
        companyInfo.IsExport = WebHelper.GetFormValue<int>("rbtnIsExport", 0);

        if (Xinkui.Common.Singleton<CompanyInfoMaintainDataAccess>.Instance.CompanyInfoMaintain_Update(companyInfo)) {
            LoginUserEntity loginUser = UserManager.Login(userInfo.UserName, userInfo.Password);
            if (Session["RUserInfo"] != null) {
                Session.Remove("RUserInfo");
            }
            if (Session["RCompanyInfo"] != null) {
                Session.Remove("RCompanyInfo");
            }
            Response.Redirect("registerstep3.aspx");
        }
        else {
            UIHelper.ShowSuucessTips("Register Error");
        }
    }
}